package www.openteam.com.workorder;


import android.util.Log;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.Inputer;
import www.openteam.com.bean.MachineInfo;
import www.openteam.com.bean.PDistrubition;
import www.openteam.com.bean.UpdateInfo;
import www.openteam.com.bean.WorkOrder;
import www.openteam.com.bean.WorkOrder2;
import www.openteam.com.exception.ApiException;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by kylin on 2017/5/2.
 */

public class BaseWorkOrderModel extends BaseModel {

    private boolean isNameExist = false;

    private boolean isQueryW = false;

    private boolean isfindM = false;

    private boolean isUpdate = false;
    private boolean isFindD = false;
    private boolean isInsert = false;
    private String TAG = getClass().getSimpleName();

    public boolean queryUserIsExist(String name, final WorkOrderOnListener workOrderOnListener) {

        httpApiService.queryUserIsExist(name)
                .compose(new CommonTransformer<Inputer>())
                .subscribe(new CommonSubscriber<Inputer>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(Inputer inputer) {
                        isNameExist = true;
                        workOrderOnListener.successInfo(inputer);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isNameExist = false;
                        workOrderOnListener.failInfo(e.message);
                    }
                });

        return isNameExist;
    }


    public boolean queryWo(String barCode, String stype, String morecom, int proCode,final
    WorkOrderWOnListener workOrderWOnListener) {

        httpApiService.queryWO(barCode, stype, morecom,proCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<WorkOrder2>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        workOrderWOnListener.failWInfo(e.getMessage());
                    }

                    @Override
                    public void onNext(WorkOrder2 workOrder2) {
                        workOrderWOnListener.suceessWInfo(workOrder2);
                    }
                });


        return isQueryW;
    }


    public boolean findMachine(String code, String inputer, String processid
            , String rdsno, String stype, String morecom, final WorkOrderMOnListener workOrderMOnListener) {

        httpApiService.findMacheine(code, inputer, processid, rdsno, stype, morecom)
                .compose(new CommonTransformer<MachineInfo>())
                .subscribe(new CommonSubscriber<MachineInfo>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(MachineInfo machineInfo) {
                        isfindM = true;
                        workOrderMOnListener.suceessMInfo(machineInfo);
                    }


                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isfindM = false;
                        workOrderMOnListener.failMInfo(e.message);
                    }
                });

        return isfindM;


    }

    public boolean updateTimeStatus(String zbid,
                                    String procno,
                                    String id,
                                    String e_Time, String stype, String morecom, final WorkOrderUOnListener workOrderUOnListener) {
        httpApiService.updateTimeStatus(zbid, procno, id, e_Time, stype, morecom)
                .compose(new CommonTransformer<UpdateInfo>())
                .subscribe(new CommonSubscriber<UpdateInfo>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(UpdateInfo updateInfo) {
                        isUpdate = true;
                        workOrderUOnListener.suceessUInfo(updateInfo);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        isUpdate = false;
                        workOrderUOnListener.failUInfo(e.message);

                    }
                });

        return isUpdate;
    }

    public boolean findWorkPrice(String machinecode, String banzu, String peopleGroup, String stype, String morecom, final WorkOrderDisdOnListener workOrderDisdOnListener) {


        httpApiService.findWorkPrice(machinecode, banzu, peopleGroup, stype, morecom)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PDistrubition>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        workOrderDisdOnListener.failDisdInfo(e.getMessage());
                    }

                    @Override
                    public void onNext(PDistrubition pDistrubition) {
                        workOrderDisdOnListener.suceessDisdInfo(pDistrubition);
                    }
                });
        return isFindD;
    }

    /**
     * @param InputDate 建单日期
     * @param zbid      生产工程单id
     * @param rdsno     工单编号
     * @param orderno   订单编号
     * @param custid    客户id
     * @param StartTime 开始时间
     * @param PartID    部件id
     * @param PrdID     生产工程单工序id
     * @param TolNumb   应产数
     * @param code      产品编号
     * @param Inputer   錄入员
     * @param UserID    登录的用户id
     * @param MachinID  机台编号
     * @param procno    工序编号
     * @return
     */
    public boolean insertSProductionInfo(
            long InputDate,
            String zbid,
            String rdsno,
            String orderno,
            String custid,
            long StartTime,
            String PartID,
            String PrdID,
            double TolNumb,
            String code,
            String Inputer,
            String UserID,
            String MachinID,
            String BuilderName,
            String InputerName,
            String procno,
            String stype,
            String morecom,
            final WorkOrderSPOnListener workOrderSPOnListener) {

        httpApiService.insertSProductionInfo(InputDate, zbid, rdsno, orderno, custid, StartTime,
                PartID, PrdID, TolNumb, code, Inputer, UserID, MachinID, BuilderName, InputerName, procno
                , stype, morecom)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<UpdateInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        isInsert = false;
                        workOrderSPOnListener.failSPInfo(e.getMessage());
                    }

                    @Override
                    public void onNext(UpdateInfo info) {
                        isInsert = true;
                        Log.e(TAG, "onNext: "+info.getInfo() );
                        workOrderSPOnListener.suceessSPInfo(info);
                    }
                });
        return isInsert;
    }


    /**
     * @param groupA     人组数据
     * @param isFinish   是否完成
     * @param rdsno      工单编号
     * @param GroupCount 人组
     * @param zbid       生产工程单id
     * @param OverDate   完成日期
     * @param EndTime    结束日期
     * @param PrdID      生产工程单工序id
     * @param Numb1      车头数
     * @param MachinID   机台编号
     * @param code       产品编号
     * @param Inputer    錄入员
     * @param procno     工序编号
     * @return
     */
    public boolean insertEProductionInfo(
            String groupA,
            boolean isFinish,
            String rdsno,
            String GroupCount,
            String zbid,
            long OverDate,
            long EndTime,
            String PrdID,
            double Numb1,
            String MachinID,
            String code,
            String Inputer,
            String procno,
            String custid,
            String orderno,
            double TolNumb,
            String PartID,
            String BuilderName,
            String InputerName,
            int userid,
            int id,
            String banzu
            , String stype, String morecom,
            String remark
            , final WorkOrderEPOnListener workOrderEPOnListener
    ) {
        base_httpApiService.insertEProductionInfo(groupA, isFinish, rdsno,
                GroupCount, zbid, OverDate, EndTime, PrdID, Numb1, MachinID, code, Inputer, procno,
                custid, orderno, TolNumb, PartID, BuilderName, InputerName, userid, id, banzu, stype, morecom,
               remark)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<UpdateInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        isInsert = false;
                        workOrderEPOnListener.failEPInfo(e.getMessage());
                    }

                    @Override
                    public void onNext(UpdateInfo info) {
                        isInsert = true;
                        workOrderEPOnListener.suceessEPInfo(info);
                    }
                });
        return isInsert;
    }


    public interface WorkOrderSPOnListener {

        void suceessSPInfo(UpdateInfo updateInfo);

        void failSPInfo(String error);

    }

    public interface WorkOrderEPOnListener {

        void suceessEPInfo(UpdateInfo updateInfo);

        void failEPInfo(String error);

    }


    public interface WorkOrderMOnListener {

        void suceessMInfo(MachineInfo machineInfo);

        void failMInfo(String error);

    }


    public interface WorkOrderOnListener {

        void successInfo(Inputer inputer);

        void failInfo(String error);


    }

    public interface WorkOrderWOnListener {

        void suceessWInfo(WorkOrder2 workOrder);

        void failWInfo(String error);


    }

    public interface WorkOrderUOnListener {

        void suceessUInfo(UpdateInfo updateInfo);

        void failUInfo(String error);


    }

    /**
     * 查找分配方案的接口
     */
    public interface WorkOrderDisdOnListener {

        void suceessDisdInfo(PDistrubition pDistrubition);

        void failDisdInfo(String error);


    }


}
